import { ref } from "vue"
import store from "@/store"
import { defineStore } from "pinia"
import { usePermissionStore } from "./permission"
import { useTagsViewStore } from "./tags-view"
import { useSettingsStore } from "./settings"
import { getToken, removeToken, setToken } from "@/utils/cache/cookies"
import router, { resetRouter } from "@/router"
import { loginApi, getUserInfoApi } from "@/api/login"
import { type LoginRequestData } from "@/api/login/types/login"
import { type RouteRecordRaw } from "vue-router"
import routeSettings from "@/config/route"
import { useAppStore } from "./app"
import Cookies from 'js-cookie'
import { fa } from "element-plus/es/locales.mjs"
export const useUserStore = defineStore("user", () => {
  const token = ref<string>(getToken() || "")
  const roles = ref<string[]>([])
  const username = ref<string>("")
  const appStore = useAppStore()

  const permissionStore = usePermissionStore()
  const tagsViewStore = useTagsViewStore()
  const settingsStore = useSettingsStore()

  /** 设置角色数组 */
  const setRoles = (value: string[]) => {
    roles.value = value
  }
  /** 登录 */
  const login = async ({ mobile, sms }: LoginRequestData) => {

    const { code } = await loginApi({ mobile, sms })
    // let zwchat = Cookies.get("TDC_itoken")
    let TOKEN = Cookies.get("zwchat")


    if (code === 1 && TOKEN) {
      console.log(TOKEN);
      // setToken(TOKEN)
      token.value = TOKEN
      appStore.closeSidebar(false)
    }
    return code
  }
  // /** 获取用户详情 */
  // const getInfo = async () => {
  //   const { data } = await getUserInfoApi()
  //   username.value = data.phone
  //   // 验证返回的 roles 是否为一个非空数组，否则塞入一个没有任何作用的默认角色，防止路由守卫逻辑进入无限循环
  //   roles.value = data.roles?.length > 0 ? data.roles : routeSettings.defaultRoles
  // }
  // /** 切换角色 */
  // const changeRoles = async (role: string) => {
  //   const newToken = "token-" + role
  //   token.value = newToken
  //   setToken(newToken)
  //   await getInfo()
  //   permissionStore.setRoutes(roles.value)
  //   resetRouter()
  //   permissionStore.dynamicRoutes.forEach((item: RouteRecordRaw) => {
  //     router.addRoute(item)
  //   })
  //   _resetTagsView()
  // }
  /** 登出 */
  const logout = () => {
    removeToken()
    token.value = ""
    roles.value = []
    resetRouter()
    _resetTagsView()
  }
  /** 重置 Token */
  const resetToken = () => {
    removeToken()
    token.value = ""
    roles.value = []
  }
  /** 重置 Visited Views 和 Cached Views */
  const _resetTagsView = () => {
    if (!settingsStore.cacheTagsView) {
      tagsViewStore.delAllVisitedViews()
      tagsViewStore.delAllCachedViews()
    }
  }

  return { token, roles, username, setRoles, login, logout, resetToken }
})

/** 在 setup 外使用 */
export function useUserStoreHook() {
  return useUserStore(store)
}
